package com.momo.hr.controller.system.basic;

import com.momo.hr.bean.Department;
import com.momo.hr.bean.RespBean;
import com.momo.hr.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制器
 */
@RestController
@RequestMapping("/system/basic/department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    /**
     * 获取所有的部门信息
     * @return Department
     */
    @GetMapping("/")
    public List<Department> getAllDepartments(){
        return this.departmentService.getAllDepartments();
    }

    /**
     * 添加部门的信息，
     * @param department
     * @return RespBean
     */
    @PostMapping("/")
    public RespBean addDep(@RequestBody Department department){
        this.departmentService.addDepartment(department);
        if(department.getResult() == 1){
            return RespBean.ok("添加成功",department);
        }
        return RespBean.error("添加失败");
    }


    @DeleteMapping("/{id}")
    public RespBean deleteDep(@PathVariable Integer id){
        Department dep = new Department();
        dep.setId(id);
        departmentService.deleteDepById(dep);
        if(dep.getResult() == -2){
           return RespBean.error("删除失败，该部门下有子部门");
        } else if (dep.getResult() == -1){
           return RespBean.error("该部门下还有对应员工下");
        }
        return RespBean.ok("删除成功");
    }

}
